<template>
    <basePages>
        <view class="home-container">
            <!-- 顶部欢迎区域 -->
            <view class="welcome-section">
                <view class="welcome-text">
                    <text class="greeting">您好，{{ userStore.userData?.name || '用户' }}</text>
                    <text class="sub-greeting">欢迎使用老年照护系统</text>
                </view>
                <view class="date-info">
                    <text class="date">{{ currentDate }}</text>
                    <!-- <text class="weather">晴朗 26°C</text> -->
                </view>
            </view>

            <!-- 快捷功能区 -->
            <view class="quick-actions">
                <view class="action-card" @click="navigateTo('/pages/common/healthCondition')">
                    <view class="icon-wrapper health">
                        <uni-icons type="heart" size="30" color="#fff"></uni-icons>
                    </view>
                    <text class="action-text">健康监测</text>
                </view>
                <view class="action-card" @click="navigateTo('/pages/common/alert')">
                    <view class="icon-wrapper alert">
                        <uni-icons type="notification" size="30" color="#fff"></uni-icons>
                    </view>
                    <text class="action-text">警报中心</text>
                </view>
                <view class="action-card" @click="navigateTo('/pages/common/record')">
                    <view class="icon-wrapper record">
                        <uni-icons type="calendar" size="30" color="#fff"></uni-icons>
                    </view>
                    <text class="action-text">健康档案</text>
                </view>
                <view class="action-card" @click="navigateTo('/pages/common/profile')">
                    <view class="icon-wrapper profile">
                        <uni-icons type="person" size="30" color="#fff"></uni-icons>
                    </view>
                    <text class="action-text">个人信息</text>
                </view>
            </view>

            <!-- 健康状态卡片 -->
            <view class="health-status">
                <view class="card-header">
                    <text class="card-title">今日健康状态</text>
                    <text class="more" @click="navigateTo('/pages/common/healthCondition')">查看更多</text>
                </view>
                <view class="status-content">
                    <view class="status-item">
                        <text class="label">心率</text>
                        <text class="value">{{healthData.getRealtimeHealthData.heartRate}}</text>
                        <text class="unit">次/分</text>
                    </view>
                    <view class="status-item">
                        <text class="label">血氧</text>
                        <text class="value">{{healthData.getRealtimeHealthData.bloodOxygen}}</text>
                        <text class="unit">%</text>
                    </view>
                    <view class="status-item">
                        <text class="label">血压</text>
                        <text class="value">{{healthData?.getRealtimeHealthData?.bloodPressureHigh+ '/'+ healthData?.getRealtimeHealthData?.bloodPressureLow}}</text>
                        <text class="unit">mmHg</text>
                    </view>
                </view>
            </view>

            <!-- 最近活动 -->
            <view class="recent-activities">
                <view class="card-header">
                    <text class="card-title">最近活动</text>
                </view>
                <view class="activity-list">
                    <view class="activity-item">
                        <view class="activity-icon">
                            <uni-icons type="medication" size="24" color="#409EFF"></uni-icons>
                        </view>
                        <view class="activity-info">
                            <text class="activity-title" @click="medicineRecord">用药提醒</text>
                            <text class="activity-time">10:30</text>
                        </view>
                    </view>
                    <view class="activity-item">
                        <view class="activity-icon">
                            <uni-icons type="heart" size="24" color="#67C23A"></uni-icons>
                        </view>
                        <view class="activity-info">
                            <text class="activity-title" @click="healthRecord">健康检查</text>
                            <text class="activity-time">09:15</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </basePages>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '../../../store/modules/useUserStore'
import { useHealthData } from '../../../store/modules/useHealthData'

const userStore = useUserStore()
const healthData = useHealthData()

const currentDate = ref(new Date().toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
}))

const medicineRecord = () => {
    uni.navigateTo({
        url: '/pages/role/older/medicineRemind'
    })
}

const healthRecord = () => {
    uni.navigateTo({
        url: '/pages/common/record'
    })
}
const navigateTo = (url) => {
    uni.navigateTo({ url })
}

onMounted(() => {

    console.log('Home page mounted')
    
    try {
        healthData.setCurUserId(userStore.userData.id)
        healthData.fetchRealtimeData()
    } catch (error) {
        console.log('Home page mounted error:', error)
        throw error
    }


})
</script>

<style lang="scss" scoped>
.home-container {
    min-height: 100vh;
    background-color: #F5F9FF;
    padding: 30rpx;
}

.welcome-section {
    background: linear-gradient(135deg, #409EFF 0%, #2987FE 100%);
    border-radius: 20rpx;
    padding: 40rpx;
    color: #fff;
    margin-bottom: 30rpx;
    box-shadow: 0 4rpx 12rpx rgba(64, 158, 255, 0.2);

    .welcome-text {
        margin-bottom: 20rpx;

        .greeting {
            font-size: 40rpx;
            font-weight: bold;
            display: block;
            margin-bottom: 10rpx;
        }

        .sub-greeting {
            font-size: 28rpx;
            opacity: 0.9;
        }
    }

    .date-info {
        display: flex;
        justify-content: space-between;
        font-size: 24rpx;
        opacity: 0.8;
    }
}

.quick-actions {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20rpx;
    margin-bottom: 30rpx;

    .action-card {
        background: #fff;
        border-radius: 16rpx;
        padding: 20rpx;
        text-align: center;
        box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

        .icon-wrapper {
            width: 80rpx;
            height: 80rpx;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 10rpx;

            &.health {
                background: #409EFF;
            }

            &.alert {
                background: #F56C6C;
            }

            &.record {
                background: #67C23A;
            }

            &.profile {
                background: #E6A23C;
            }
        }

        .action-text {
            font-size: 24rpx;
            color: #333;
        }
    }
}

.health-status {
    background: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    margin-bottom: 30rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20rpx;

        .card-title {
            font-size: 32rpx;
            font-weight: bold;
            color: #333;
        }

        .more {
            font-size: 24rpx;
            color: #409EFF;
        }
    }

    .status-content {
        display: flex;
        justify-content: space-between;

        .status-item {
            text-align: center;

            .label {
                font-size: 24rpx;
                color: #666;
                display: block;
                margin-bottom: 10rpx;
            }

            .value {
                font-size: 36rpx;
                font-weight: bold;
                color: #333;
            }

            .unit {
                font-size: 24rpx;
                color: #999;
                margin-left: 4rpx;
            }
        }
    }
}

.recent-activities {
    background: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

    .card-header {
        margin-bottom: 20rpx;

        .card-title {
            font-size: 32rpx;
            font-weight: bold;
            color: #333;
        }
    }

    .activity-list {
        .activity-item {
            display: flex;
            align-items: center;
            padding: 20rpx 0;
            border-bottom: 1rpx solid #F0F7FF;

            &:last-child {
                border-bottom: none;
            }

            .activity-icon {
                width: 80rpx;
                height: 80rpx;
                background: #F0F7FF;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                margin-right: 20rpx;
            }

            .activity-info {
                flex: 1;

                .activity-title {
                    font-size: 28rpx;
                    color: #333;
                    display: block;
                }

                .activity-time {
                    font-size: 24rpx;
                    color: #999;
                }
            }
        }
    }
}
</style>